package com.kunbo.cn.app.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.app.dao.DriverDao;
import com.kunbo.cn.app.entity.Driver;
import com.kunbo.cn.app.service.DriverService;

@Service
public class DriverImpl implements DriverService{
	
	@Autowired
	private DriverDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	

	@Override
	public Boolean saveDriver(String number, String name, String idcrad, String contact_number,
			String drivers_license,String uuid,String create_user_id,String unit) {
		// TODO Auto-generated method stub
		Driver driver = new Driver();
		driver.setNumber(number);
		driver.setName(name);
		driver.setIdcrad(idcrad);
		driver.setContact_number(contact_number);
		driver.setDrivers_license(drivers_license);
		driver.setUuid(uuid);
		driver.setSubordinate_transport_units(unit);
		driver.setCreate_user_id(create_user_id);
		driver.setDriver_state("空闲");
		driver = dao.save(driver);
		if(driver != null)
			return true;
		else
			return false;
	}

	@Override
	public Boolean updateDriver(Integer id, String number, String name, String idcrad, String contact_number,
			String drivers_license) {
		// TODO Auto-generated method stub
		Driver driver = dao.findOne(id);
		driver.setNumber(number);
		driver.setName(name);
		driver.setIdcrad(idcrad);
		driver.setContact_number(contact_number);
		driver.setDrivers_license(drivers_license);
		driver = dao.save(driver);
		if(driver != null)
			return true;
		else
			return false;
	}

	@Override
	public Driver selById(Integer id) {
		// TODO Auto-generated method stub
		return dao.findOne(id);
	}

	@Override
	public Boolean delById(Integer id) {
		// TODO Auto-generated method stub
		dao.delete(id);
		Driver driver = dao.findOne(id);
		if(driver == null)
			return true;
		else
			return false;
	}

	@Override
	public List<Map<String,Object>> selDriver(Integer wayBillUnit) {
		// TODO Auto-generated method stub
		String unit = "";
		String unitSql = "select name_of_transport_unit from transport_unit_management where id = " + wayBillUnit;
		if(jdbc.queryForList(unitSql) != null && jdbc.queryForList(unitSql).size()>0) {
			unit = jdbc.queryForList(unitSql).get(0).get("name_of_transport_unit").toString();
		}
		String sql = "select * from driver_management where subordinate_transport_units = '" + unit + "' order by id desc";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> selDriverByName(String name,Integer wayBillUnit) {
		// TODO Auto-generated method stub
		String unit = "";
		String unitSql = "select name_of_transport_unit from transport_unit_management where id = " + wayBillUnit;
		if(jdbc.queryForList(unitSql) != null && jdbc.queryForList(unitSql).size()>0) {
			unit = jdbc.queryForList(unitSql).get(0).get("name_of_transport_unit").toString();
		}
		String sql = "select * from driver_management where name like '%" +name+ "%' and subordinate_transport_units = '" + unit +"' order by id desc";
		return jdbc.queryForList(sql);
	}

	@Override
	public String selPhoneById(Integer id) {
		// TODO Auto-generated method stub
		String sql = "select number from driver_management where id = " + id;
		return jdbc.queryForList(sql).get(0).get("number").toString();	
		}

	@Override
	public List<Map<String, Object>> getAllDrivers(String idcrad) {
		String sql = "select name,idcrad,contact_number from driver_management  where driver_state = '空闲'  ";
		if(StringUtils.isNotBlank(idcrad)) {
			sql+= " and idcrad like '%"+idcrad+"%'";
		}
		return jdbc.queryForList(sql);
	}
}
